package com.ff.xyh.service.post.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.vo.post.PostShopVo;
import com.ff.xyh.common.vo.post.PostTravelVo;
import com.ff.xyh.common.qo.post.PostShopQuery;
import com.ff.xyh.common.qo.post.PostTravelQuery;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.post.PostShop;
import com.ff.xyh.common.entity.post.PostTravel;
import com.ff.xyh.service.post.service.PostShopService;
import com.ff.xyh.service.post.service.PostTravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther FF
 * @Date 2022年 10月 27日
 * @Time 01:24
 */

@RestController
@RequestMapping("/post-service")
public class PostController {
    @Autowired
    private PostShopService postShopService;
    @Autowired
    private PostTravelService postTravelService;


    /**
     * 获取所有拼购帖
     * @return
     */
    @GetMapping("get-post-shop-all")
    public ResultObject getAllPostShop (){
        List<PostShop> postShopList = postShopService.list(null);
        if (postShopList == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", postShopList);
            return ResultObject.ok().data(map);
        }
    }
    /**
     * 获取所有旅游帖
     * @return
     */
    @GetMapping("get-post-travel-all")
    public ResultObject getAllPostTravel (){
        List<PostTravel> postTravelList = postTravelService.list(null);
        if (postTravelList == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", postTravelList);
            return ResultObject.ok().data(map);
        }
    }

    /**
     * 分页查询 拼购贴 用于无条件查询拼购帖
     * @param currentPage 页数
     * @param size 页面大小
     * @return
     */
    @GetMapping("get-post-shop-page")
    public ResultObject getPostShopPage (
        @RequestParam(name = "current") long currentPage,
        @RequestParam(name = "size") long size) {

        Page<PostShop> postShopPage = new Page<>(currentPage,size);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        postShopService.page(postShopPage, null);

        long total = postShopPage.getTotal();//总记录数
        List<PostShop> postShopList = postShopPage.getRecords(); //数据list集合
        if (postShopList == null || postShopList.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("total", total);
            map.put("items", postShopList);
            return ResultObject.ok().data(map);
        }
    }

    /**
     * 分页查询 旅游帖
     * @param currentPage 页数
     * @param size 页面大小
     * @return
     */
    @GetMapping("get-post-travel-page")
    public ResultObject getPostTravelPage (
        @RequestParam(name = "current") long currentPage,
        @RequestParam(name = "size") long size) {

        Page<PostTravel> postTravelPage = new Page<>(currentPage,size);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        postTravelService.page(postTravelPage, null);

        long total = postTravelPage.getTotal();//总记录数
        List<PostTravel> postTravelList = postTravelPage.getRecords(); //数据list集合
        if (postTravelList == null || postTravelList.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("total", total);
            map.put("items", postTravelList);
            return ResultObject.ok().data(map);
        }
    }

    /**
     * 根据用户id和帖子类型查询用户所有帖子
     * @param userId
     * @param type
     * @return
     */
    @GetMapping("get-post-user-type")
    public ResultObject getPostByUserIdAndType(
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "type") int type) {
        if (StringUtils.isEmpty(userId))
            return ResultObject.error().message(ResultMessage.ERROR_ARG);

        if (type == 0){
            return ResultObject.error().message(ResultMessage.UNKNOWN_TYPE);
        } else if (type == 1) {
            // 拼购帖子
            List<PostShop> postShopList = postShopService.list(
                    new QueryWrapper<PostShop>().eq("user_id", userId));
            if (postShopList == null || postShopList.isEmpty()) {
                return ResultObject.error().message(ResultMessage.NO_RESULT);
            } else {
                Map map = new HashMap();
                map.put("items", postShopList);
                return ResultObject.ok().data(map);
            }
        } else if (type == 2) {
            // 旅游帖子
            List<PostTravel> postTravelList = postTravelService.list(
                    new QueryWrapper<PostTravel>().eq("user_id", userId));
            if (postTravelList == null || postTravelList.isEmpty()) {
                return ResultObject.error().message(ResultMessage.NO_RESULT);
            } else {
                Map map = new HashMap();
                map.put("items", postTravelList);
                return ResultObject.ok().data(map);
            }

        } else if (type == 3) {
            return ResultObject.error().message("未开放");
        } else {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
    }

    /**
     * 条件查询拼购帖 包含分页
     * @param postShopQuery
     * @return
     */
    @PostMapping("get-post-shop-condition")
    public ResultObject getPostShopPageCondition(
            @RequestBody PostShopQuery postShopQuery) {


        return postShopService
                .getPostShopPageCondition(postShopQuery);


//        System.out.println(postShopQuery);
//        postShopQuery.changeCurrentPageAndSizeToLimitIndex(postShopQuery);
//        System.out.println(postShopQuery);
//
//
//        List<PostShop> postShopList = postShopService.getPostShopPageCondition(postShopQuery);
//        // 将size设置为0 再次查询所有满足条件的数据 得到行数
//        long total = postShopService.getPostShopPageCondition(postShopQuery.setSize(0)).size();
//        if (postShopList == null || postShopList.isEmpty()) {
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        } else {
//            Map map = new HashMap();
//            map.put("total", total);
//            map.put("items", postShopList);
//            return ResultObject.ok().data(map);
//        }
    }
    /**
     * 条件查询旅游帖 包含分页
     * @param postTravelQuery
     * @return
     */
    @PostMapping("get-post-travel-condition")
    public ResultObject getPostTravelPageCondition(@RequestBody PostTravelQuery postTravelQuery) {
        return postTravelService.getPostTravelPageCondition(postTravelQuery);
    }


    @GetMapping("get-post-shop-id")
    public ResultObject getPostShopById(
            @RequestParam(name = "id") String id) {
        PostShop data = postShopService.getById(id);
        if (data == null)
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        else
            return ResultObject.ok().data("data", data);
    }

    @GetMapping("get-post-travel-id")
    public ResultObject getPostTravelById(
            @RequestParam(name = "id") String id) {
        PostTravel data = postTravelService.getById(id);
        if (data == null)
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        else
            return ResultObject.ok().data("data", data);
    }

    @GetMapping("get-post-shop-ids")
    public ResultObject getPostShopByIds(
            @RequestParam(name = "ids") List<String> ids) {
        List<PostShop> postShops = postShopService.listByIds(ids)
                .stream().collect(Collectors.toList());

        if (postShops == null || postShops.isEmpty())
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        else
            return ResultObject.ok().data("items", postShops);
    }

    @GetMapping("get-post-travel-ids")
    public ResultObject getPostTravelByIds(
            @RequestParam(name = "ids") List<String> ids) {
        List<PostTravel> postTravels = postTravelService.listByIds(ids)
                .stream().collect(Collectors.toList());

        if (postTravels == null || postTravels.isEmpty())
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        else
            return ResultObject.ok().data("items", postTravels);
    }

    // TODO 添加购物贴 写入对应post_shop表 同时将照片集 写入post_img表 若开启群聊 则同时需要新建 post_room表数据
    // TODO 后期可以通过设置 是否开启群聊功能 收取服务费
    @PostMapping("insert-post-shop")
    public ResultObject insertPostShop (
            @RequestBody PostShopVo postShopVo) {
        PostShop postShop = postShopVo.getPost();
        postShop = postShopService.insertPostShop(postShop, postShopVo.getLevel());
        if (postShop != null) {
            Map map = new HashMap();
            map.put("data", postShop);
            return ResultObject.ok().data(map);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }
    // TODO
    @PostMapping("insert-post-travel")
    public ResultObject insertPostTravel (
            @RequestBody PostTravelVo postTravelVo) {
        PostTravel postTravel = postTravelVo.getPost();
        postTravel = postTravelService.insertPostTravel(postTravel, postTravelVo.getLevel());
        if (postTravel != null) {
            Map map = new HashMap();
            map.put("data", postTravel);
            return ResultObject.ok().data(map);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }

    @PostMapping("update-post-shop")
    public ResultObject updatePostShop (
            @RequestBody PostShop postShop) {
        if (postShopService.getById(postShop.getId()) == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
        if (postShopService.updateById(postShop)) {
            Map map = new HashMap();
            map.put("data", postShop);
            return ResultObject.ok().data(map);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }
    @PostMapping("update-post-travel")
    public ResultObject updatePostTravel (
            @RequestBody PostTravel postTravel) {
        if (postTravelService.updateById(postTravel)) {
            Map map = new HashMap();
            map.put("data", postTravel);
            return ResultObject.ok().data(map);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }

    /**
     * 删除帖子 需要id和类型 通过消费者服务调用 调用前需要删除帖子保存的图片以及其他表相关信息
     * @param id
     * @param type
     * @return
     */
    @GetMapping("delete-post-id-type")
    public ResultObject deletePostByIdAndType (
            @RequestParam(name = "id") String id,
            @RequestParam(name = "type") int type) {
        if (type == 0){
            return ResultObject.error().message(ResultMessage.UNKNOWN_TYPE);
        } else if (type == 1) {
            // 拼购帖子
            PostShop postShop = postShopService.getById(id);
            if (postShop == null) {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
            } else {
                if (postShopService.removeById(id)) {
                    Map map = new HashMap();
                    map.put("data", postShop);
                    return ResultObject.ok().data(map);
                } else {
                    return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
                }
            }
        } else if (type == 2) {
            // 旅游帖子
            PostTravel postTravel = postTravelService.getById(id);
            if (postTravel == null) {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
            } else {
                if (postTravelService.removeById(id)) {
                    Map map = new HashMap();
                    map.put("data", postTravel);
                    return ResultObject.ok().data(map);
                } else {
                    return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
                }
            }

        } else if (type == 3) {
            return ResultObject.error().message("未开放");
        } else {
            return ResultObject.error().message(ResultMessage.UNKNOWN_TYPE);
        }
    }






}
